package org.example.DesignPatterns.Command;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 作为命令请求方的键盘控制类
 * @author: farprt
 * @create: 2022-09-24 14:04
 **/
public class Keyboard {

    public enum KeyCode{
        F1,F2,ESC,UP,DOWN,LEFT,RIGHT;
    }

    private Map<KeyCode, List<Command>> keyCommands=new HashMap<>();

    //按键与命令映射
    public void bindKeyCommand(KeyCode keyCode,List<Command> commands){
        this.keyCommands.put(keyCode,commands);
    }

    public void onKeyPressed(KeyCode keyCode){
        System.out.println(keyCode+"键按下");
        List<Command> commands=this.keyCommands.get(keyCode);
        if(commands==null){
            System.out.println("警告，无效的命令");
            return;
        }
        commands.stream().forEach(command -> command.exe());
    }

}